from pathlib import Path
from configparser import ConfigParser


class ReadConfig(object):
    def __init__(self, file_path: Path):
        super(ReadConfig, self).__init__()
        self.config = self.read_file(file_path)

    def read_file(self, file_path: Path):
        config = ConfigParser()
        if file_path.is_file(): config.read(file_path, encoding='utf-8')
        else:
            config['BASE'] = {'video_wide': '', 'video_high': '', 'group_num': '5', 'min_time': '180',
                              'max_frames': '90', 'max_workers': '6',}
            config['FFMPEG'] = {'path': 'ffmpeg', 'loglevel': 'error', 'hwaccel': 'cuda', 'threads': '4',
                                'c_v': 'h264_nvenc', 'b_v': '2000k', 'minrate': '2000k', 'bufsize': '2000k',
                                'rc': 'vbr', 'preset': 'fast', 'ar': '44100', 'c_a': 'aac', 'b_a': '128k',
                                'movflags': '+faststart', 'min_file': '0', 'max_file': '10', 'transition': 'slideleft',}
            config['DIR_PATH'] = {'in_dir': 'video', 'out_dir': 'output'}
            config['EXTS'] = {'video': ','.join([".mp4", ".avi", ".mov", ".mkv", ".flv", ".wmv", ".rmvb", ".3gp"])}
            self.save_file(file_path, config)
        return config

    def save_file(self, file_path: Path, config: ConfigParser):
        with open(file_path, 'w', encoding='utf-8') as fp:
            config.write(fp)

